#pragma once
#include <iostream>
#include <string>
#include <queue>
#include <QVariant>
#include <tableManager.h>
#include "view.h"
#include "aggHelper.h"
#include "logger.h"
using namespace std;

class dbProcess
{
     static table* countTable;
public:
     static queue<processObject> processQueue;

     static queue<processObject> correspondQueue;

    static void setCount(table* table)
    {
        table->setSystemManage();
        countTable=table;
    }

    static bool checkCount(const processObject& obj){
        string userName=obj.getUser();
        string passWord=obj.getPassWord();
        vector<int> countVec=countTable->find({"(x=='"+userName+"')","(x=='"+passWord+"')"});
        if(countVec.empty()){
            return false;
        }
        return true;
    }

    static void processRequst(){
        if(processQueue.empty()){
            return;
        }
        processObject tmpProcess=processQueue.front();
        processQueue.pop();
        tableManager::setcurOperatUser(tmpProcess.getUser());
        if(checkCount(tmpProcess)==false){
            tmpProcess.setResult("Account password error");
        }
        else
        {
            QString mistake;
            QVariant varResult=JSEval(QString::fromStdString(tmpProcess.getJS()),"eval",&mistake,view::viewmanager->getViewJSVM());
            if(varResult==NULL)
            {
                tmpProcess.setResult(mistake.toStdString());
            }
            else
            {
                tmpProcess.setResult(varResult.toString().toStdString());
            }
        }
        if(tmpProcess.getResult()==""){
            tmpProcess.setResult("Run successfully");
        }
        correspondQueue.push(tmpProcess);

        #ifdef StartLogger
        logger::writeLog(tmpProcess);
        #endif

        tableManager::tablemanager->doManage();

        #ifdef autoRecovery
        manageContain::reset();
        #endif
    }
};
